package com.zsrt.oa.user.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.zsrt.oa.user.dao.UserDao;
import com.zsrt.oa.user.dao.impl.UserDaoImpl;
import com.zsrt.oa.user.entity.UserEntity;
import com.zsrt.oa.user.service.UserService;

/**
 * 这是用户服务层的实现类
 * @author 罗智壕-13424431416
 *
 * 2017年6月8日
 */
public class UserServiceImpl implements UserService {
	
	/**
	 * 初始化用户的持久层
	 */
	private UserDao userDao = new UserDaoImpl();
	
	/**
	 * 用户注册判断
	 * @param userPhone	需要判断的手机号码
	 * @param map		要返回前天的集合
	 */
	@Override
	public void registerGudge(String userPhone, Map<String, Object> map) {
		UserEntity entity = new UserEntity();
		entity.setUserPhone(userPhone);
		try {
			int result = userDao.registerGudge(entity);
			if(result == 1){
				map.put("result", "failure");
				map.put("message", "该手机号码已经注册,请重新输入...");
			}else{
				map.put("result", "success");
			}
		} catch (SQLException e) {
			map.put("result", "error");
			map.put("message", "程序员正在努力抢修中,请稍后再试....");
			e.printStackTrace();
		}
	}
	
	/**
	 * 用户注册
	 * @param entity
	 * @return
	 * @throws SQLException
	 */
	@Override
	public void userAdd(String userName,String userPassword,String userPhone, Map<String, Object> map) {
		UserEntity entity = new UserEntity();
		entity.setUserPhone(userPhone);
		entity.setUserName(userName);
		entity.setUserPassword(userPassword);
		try {
			int result = userDao.userAdd(entity);
			if(result == 1){
				map.put("result", "success");
				map.put("message", "注册成功...");
			}else{
				map.put("result", "failure");
				map.put("message", "手机号码,已存在,请重新输入...");
			}
		} catch (SQLException e) {
			map.put("result", "error");
			map.put("message", "程序员正在努力抢修中,请稍后再试....");
			e.printStackTrace();
		}
	}
	
	/**
	 * 用户登录
	 * @param userPassword
	 * @param userPhone
	 * @param map
	 */
	@Override
	public void userLogin(String userPassword, String userPhone, Map<String, Object> map) {
		UserEntity entity = new UserEntity();
		entity.setUserPhone(userPhone);
		entity.setUserPassword(userPassword);
		try {
			int result = userDao.userLogin(entity);
			if(result == 1){
				map.put("result", "success");
			}else{
				map.put("result", "failure");
				map.put("message", "登录失败,手机号码或密码错误...");
			}
		} catch (SQLException e) {
			map.put("result", "error");
			map.put("message", "程序员正在努力抢修中,请稍后再试....");
			e.printStackTrace();
		}
	}
	
	
	
	/**
	 * 当前用户信息
	 * @param userPhone
	 * @param map
	 */
	@Override
	public void myMessage(Object userPhone, Map<String, Object> map) {
		List<UserEntity> list = new ArrayList<UserEntity>();
		try {
			String up = (String) userPhone;
			list = userDao.myMessage(up);
			map.put("result", "success");
			map.put("data", list);
		} catch (SQLException e) {
			map.put("result", "error");
			map.put("data", list);
			map.put("message", "程序员正在努力抢修中,请稍后再试....");
			e.printStackTrace();
		}
	}
}
